package mfuny.ggongal.admin.http;

import java.lang.reflect.Method;
import java.sql.Timestamp;

import mfuny.ggongal.admin.Server;
import mfuny.ggongal.admin.report.PvDao;
import mfuny.ggongal.admin.report.PvTimeDao;

import com.ibatis.sqlmap.client.SqlMapClient;

public class HttpPv {
	SqlMapClient sqlMapper;
	
	public HttpPv() throws Exception {
		sqlMapper = Server.getSqlMapper( );
	}
	
	//노출수 추가.
	@SuppressWarnings( "deprecation" )
	public int pv_add( int ad_no, String pvType ) throws Exception {
		int result = 0;
		PvDao pvDao = new PvDao();
		Timestamp now = new Timestamp( System.currentTimeMillis( ) );
		now.setMinutes( 0 );
		now.setSeconds( 0 );
		now.setNanos( 0 );
		pvDao.setDay( now );
		pvDao.setAd_no( ad_no );
		Method[] m = pvDao.getClass( ).getMethods( );
		for ( int i = 0; i < m.length; i++ ) {
			if ( m[i].getName( ).toLowerCase( ).equals( "set" + pvType ) ) {
				m[i].invoke( pvDao, 1 );
			}
		}
		
		result = (Integer)sqlMapper.queryForObject( "pv_isFirst", pvDao );
		if ( result > 0 ) {
			result = sqlMapper.update( "pv_addUpdate", pvDao );
		} else {
			result = sqlMapper.update( "pv_add", pvDao );
		}
		
		return result;
	}
	
	//노출수 추가(꽁전용/시간별).
	@SuppressWarnings( "deprecation" )
	public int pv_ggongAdd( int ad_no ) throws Exception {
		int result = 0;
		PvTimeDao pvTimeDao = new PvTimeDao();
		Timestamp now = new Timestamp( System.currentTimeMillis( ) );
		int hour = now.getHours( );
		now.setHours( 0 );
		now.setMinutes( 0 );
		now.setSeconds( 0 );
		now.setNanos( 0 );
		pvTimeDao.setAd_no( ad_no );
		pvTimeDao.setDay( now );
		Method[] m = pvTimeDao.getClass( ).getMethods( );
		for ( int i = 0; i < m.length; i++ ) {
			if ( m[i].getName( ).equals( "setCpv" + hour )) {
				m[i].invoke( pvTimeDao, 1 );
			}
		}
		
		result = (Integer)sqlMapper.queryForObject( "pvTime_isFirst", pvTimeDao );
		if ( result > 0 ) {
			result = sqlMapper.update( "pvTime_addUpdate", pvTimeDao );
		} else {
			result = sqlMapper.update( "pvTime_add", pvTimeDao );
		}
		
		return result;
	}
}
